package com.codegeek.project.slope.controller;

import com.codegeek.common.utils.SecurityUtils;
import com.codegeek.common.utils.poi.ExcelUtil;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.framework.web.page.TableDataInfo;
import com.codegeek.project.slope.domain.SlopeSupplier;
import com.codegeek.project.slope.service.ISlopeSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 供应厂商Controller
 *
 * @author codegeek
 * @date 2020-02-17
 */
@RestController
@RequestMapping("/slope/supplier")
@Api(value="供应厂商Controller",tags={"供应厂商操作接口"})
public class SlopeSupplierController extends BaseController
{
    @Autowired
    private ISlopeSupplierService slopeSupplierService;

    /**
     * 查询供应厂商列表
     */
    @ApiOperation("查询供应厂商列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('slope:supplier:list')")
    public TableDataInfo list(SlopeSupplier slopeSupplier)
    {
        startPage();
        List<SlopeSupplier> list = slopeSupplierService.selectSlopeSupplierList(slopeSupplier);
        return getDataTable(list);
    }

    @ApiOperation("没有设置权限的供应厂商列表")
    @GetMapping("/noRoleList")
    public TableDataInfo noRoleList(SlopeSupplier slopeSupplier)
    {
        startPage();
        List<SlopeSupplier> list = slopeSupplierService.selectSlopeSupplierList(slopeSupplier);
        return getDataTable(list);
    }


//    /**
//     * 导出供应厂商列表
//     */
//    @ApiOperation("导出供应厂商列表")
//    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    @PreAuthorize("@ss.hasPermi('slope:supplier:export')")
//    public AjaxResult export(SlopeSupplier slopeSupplier)
//    {
//        List<SlopeSupplier> list = slopeSupplierService.selectSlopeSupplierList(slopeSupplier);
//        ExcelUtil<SlopeSupplier> util = new ExcelUtil<SlopeSupplier>(SlopeSupplier.class);
//        return util.exportExcel(list, "supplier");
//    }

    /**
     * 获取供应厂商详细信息
     */
    @ApiOperation("获取供应厂商详细信息")
    @GetMapping(value = "/{supplierId}")
    @PreAuthorize("@ss.hasPermi('slope:supplier:query')")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        return AjaxResult.success(slopeSupplierService.selectSlopeSupplierById(supplierId));
    }

    /**
     * 新增供应厂商
     */
    @ApiOperation("新增供应厂商")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("@ss.hasPermi('slope:supplier:add')")
    public AjaxResult add(@RequestBody SlopeSupplier slopeSupplier)
    {
        return toAjax(slopeSupplierService.insertSlopeSupplier(slopeSupplier));
    }

    /**
     * 修改供应厂商
     */
    @ApiOperation("修改供应厂商")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('slope:supplier:edit')")
    public AjaxResult edit(@RequestBody SlopeSupplier slopeSupplier)
    {
        return toAjax(slopeSupplierService.updateSlopeSupplier(slopeSupplier));
    }

    /**
     * 删除供应厂商
     */
    @ApiOperation("删除供应厂商")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{supplierIds}")
    @PreAuthorize("@ss.hasPermi('slope:supplier:remove')")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        try {
            return AjaxResult.success(slopeSupplierService.deleteSlopeSupplierByIds(supplierIds));
        } catch (Exception e) {
            return AjaxResult.error("供应商已使用，不允许删除");
        }
    }

    @ApiOperation("下载Excel")
    @GetMapping("/getExcel")
    @PreAuthorize("@ss.hasPermi('slope:supplier:import')")
    public AjaxResult getExcel() {
        ExcelUtil<SlopeSupplier> util = new ExcelUtil<SlopeSupplier>(SlopeSupplier.class);
        return util.exportExcel(null, "供应商模板");
    }

    @ApiOperation("导入Excel")
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importDevice")
    @PreAuthorize("@ss.hasPermi('slope:supplier:import')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SlopeSupplier> util = new ExcelUtil<SlopeSupplier>(SlopeSupplier.class);
        List<SlopeSupplier> deviceList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = slopeSupplierService.importDevice(deviceList, updateSupport, operName);
        return  AjaxResult.success(message);
    }

    /**
     * 禁用供应厂商
     */
    @ApiOperation("禁用设备厂商")
    @Log(title = "设备管理", businessType = BusinessType.GRANT)
    @PostMapping("/disable/{supplierIds}")
    @PreAuthorize("@ss.hasPermi('slope:supplier:disable')")
    public AjaxResult disable(@PathVariable Long[] supplierIds)
    {
        return toAjax(slopeSupplierService.disableSlopeSupplierByIds(supplierIds));
    }

    /**
     * 启用供应厂商
     */
    @ApiOperation("启用设备厂商")
    @Log(title = "设备管理", businessType = BusinessType.GRANT)
    @PostMapping("/enable/{supplierIds}")
    @PreAuthorize("@ss.hasPermi('slope:supplier:disable')")
    public AjaxResult enable(@PathVariable Long[] supplierIds)
    {
        return toAjax(slopeSupplierService.enableSlopeSupplierByIds(supplierIds));
    }

    /**
     * 检查供应商名字是否重复
     * @param supplierName
     * @return
     */
    @ApiOperation("启用设备厂商")
    @GetMapping("/checkSupplierName")
    public AjaxResult checkSupplierName(String supplierName)
    {
        return AjaxResult.success(slopeSupplierService.checkSupplierName(supplierName));
    }

}
